//
//  SAPayment3VC.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/4/24.
//

#import "SAPayment3VC.h"
#import "UILabel+NSMutableAttributedString.h"

@interface SAPayment3VC ()<UITextFieldDelegate>

@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) UILabel *proportionLab;
@property (nonatomic, strong) UIView *whiteView;
@property (nonatomic, strong) UITextField *proportionTf;
@property (nonatomic, strong) UILabel *tipLab;

@end

@implementation SAPayment3VC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 5){
        return NO;
    }
    if((textField.text.length == 0) && [string isEqualToString:@"."]) {
        textField.text = @"0";
    }
    if([textField.text containsString:@"."]) {
        if ([string isEqualToString:@"."]) {
            return NO;
        }
        NSRange ran = [textField.text rangeOfString:@"."];
        if(range.location > ran.location) {
            if([textField.text pathExtension].length > 1) {
                return NO;
            }
        }
    }
    return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

#pragma mark - event
-(void)clickedConfirmBtn {
    if (self.proportionTf.text.length == 0) {
        [Prompt popPromptViewWithMsg:@"金额不能为空" duration:2];
        return;
    }
    !self.selectBlock ?: self.selectBlock(self.proportionTf.text);
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"结算方式"];
    [self setBgImage:nil];
    [self addRightViews:@[self.confirmBtn]];
    self.view.backgroundColor = kBgColor;
    
    [self.view addSubview:self.proportionLab];
    [self.view addSubview:self.whiteView];
    [self.whiteView addSubview:self.proportionTf];
    [self.view addSubview:self.tipLab];
    [self.tipLab setAttributedStringWithSubString:@"*" color:kRedColor];
}

-(UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmBtn.frame = CGRectMake(0, 0, kScaleNum(60), kScaleNum(44));
        [_confirmBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confirmBtn.titleLabel.font = kFont(18);
        [_confirmBtn addTarget:self action:@selector(clickedConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}

-(UILabel *)proportionLab {
    if (!_proportionLab) {
        _proportionLab = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(20), kScaleNum(StatusHeight), kScaleNum(210), kScaleNum(43))];
        _proportionLab.text = @"按固定金额核算";
        _proportionLab.textAlignment = NSTextAlignmentLeft;
        _proportionLab.textColor = kLightgGrayColor;
        _proportionLab.font = kFont(16);
    }
    return _proportionLab;
}

-(UIView *)whiteView {
    if (!_whiteView) {
        _whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 43), kBaseScreenWidth, kScaleNum(43))];
        _whiteView.backgroundColor = [UIColor whiteColor];
        UILabel *titleLab =  [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(20), 0, kScaleNum(40), kScaleNum(43))];
        titleLab.font = kFont(16);
        titleLab.textColor = kBlackColor;
        titleLab.text = @"金额*";
        [_whiteView addSubview:titleLab];
        [titleLab setAttributedStringWithSubString:@"*" color:kRedColor];
    }
    return _whiteView;
}

-(UITextField *)proportionTf {
    if (!_proportionTf) {
        _proportionTf = [[UITextField alloc] initWithFrame:CGRectMake(kScaleNum(65), 0, kScaleNum(295), kScaleNum(43))];
        _proportionTf.placeholder = @"填100表示所有商品默认按100元结算";
        _proportionTf.textColor = kBlackColor;
        _proportionTf.font = kFont(16);
        _proportionTf.textAlignment = NSTextAlignmentRight;
        _proportionTf.delegate = self;
        _proportionTf.keyboardType = UIKeyboardTypeDecimalPad;
    }
    return _proportionTf;
}

-(UILabel *)tipLab {
    if (!_tipLab) {
        _tipLab = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(20), kScaleNum(StatusHeight + 86), kScaleNum(300), kScaleNum(30))];
        _tipLab.text = @"*按照固定金额结算调货费用";
        _tipLab.font = kFont(14);
        _tipLab.textColor = kLightgGrayColor;
    }
    return _tipLab;
}

@end

